import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:solar/modal/api.dart';

class DetailPage extends StatefulWidget {
  DetailPage({Key key, @required this.pageId}) : super(key: key);
  String pageId = '';
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  Map detailData = {};
  List arrList = [];

  List<Widget> initArrList(arr, flag) {
    List<Widget> arrList = [
      SizedBox(height: 10),
      Row(
        children: [
          SizedBox(width: 15),
          Text('',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.w400,
              )),
        ],
      ),
      SizedBox(height: 40),
    ];
    List.generate(arr.length, (index) {
      arrList.add(Container(
          padding: EdgeInsets.only(left: 10, right: 10, top: 10),
          width: MediaQuery.of(context).size.width / 2,
          child: InkWell(
            onTap: () => {},
            child: Column(
              children: [
                arr[index]['url'] != null
                    ? Image.network(arr[index]['url'])
                    : Text(''),
                Container(
                  child: Text(
                    arr[index]['sub'],
                    style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                        color: Colors.black),
                  ),
                ),
              ],
            ),
          )));
    });
    return arrList;
  }

  @override
  void initState() {
    EasyLoading.show();
    getJieqiDetail(widget.pageId).then((res) {
      EasyLoading.dismiss();
      setState(() {
        detailData = res;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return detailData['name'] != null
        ? Scaffold(
            appBar: AppBar(
              title: Text(detailData['name'] + '习俗介绍'),
            ),
            backgroundColor: Colors.white,
            body: detailData == {}
                ? Container()
                : SingleChildScrollView(
                    child: Column(
                      children: [
                        Image.network(detailData['detail_url']),
                        Container(
                          padding: EdgeInsets.all(10),
                          child: Text(detailData['sub']),
                        ),
                        Column(
                          children: [
                            SizedBox(height: 30),
                            Stack(
                              alignment: Alignment.center,
                              children: [
                                Image.network(
                                    'https://static-game.duodian.cn/web/img/text-center-bg.8ecec5d2.png'),
                                Text(detailData['name'] + ' · 节气养生',
                                    style: TextStyle(
                                      fontSize: 20,
                                      fontWeight: FontWeight.w500,
                                    )),
                              ],
                            ),
                            Padding(
                              padding: EdgeInsets.only(left: 10, right: 10),
                              child: Text(detailData['ys']['sub']),
                            ),
                            Wrap(
                              children: [
                                ...initArrList(detailData['ys']['mt'], false)
                              ],
                            )
                          ],
                        )
                      ],
                    ),
                  ),
          )
        : Scaffold(
            appBar: AppBar(
              title: Text(''),
            ),
            backgroundColor: Colors.white,
            body: Text(''));
  }
}
